package com.csx.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csx.common.constant.ProductConstant;
import com.csx.common.utils.PageUtils;
import com.csx.common.utils.Query;
import com.csx.gulimall.product.dao.AttrGroupDao;
import com.csx.gulimall.product.entity.AttrEntity;
import com.csx.gulimall.product.entity.AttrGroupEntity;
import com.csx.gulimall.product.service.AttrGroupService;
import com.csx.gulimall.product.service.AttrService;
import com.csx.gulimall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>()
                .eq(key != null && !"".equals(key), "attr_group_id", key)
                .or()
                .like(key != null && !"".equals(key), "attr_group_name", key);
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper.eq("catelog_id", catelogId)
            );
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatlogId(Long catlogId) {
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(
                new QueryWrapper<AttrGroupEntity>()
                        .eq("catelog_id", catlogId));
        List<AttrGroupWithAttrsVO> collect = attrGroupEntities.stream()
                .map(attrGroupEntity -> {
                    AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
                    attrGroupWithAttrsVO.setAttrGroupId(attrGroupEntity.getAttrGroupId());
                    attrGroupWithAttrsVO.setAttrGroupName(attrGroupEntity.getAttrGroupName());
                    attrGroupWithAttrsVO.setSort(attrGroupEntity.getSort());
                    attrGroupWithAttrsVO.setDescript(attrGroupEntity.getDescript());
                    attrGroupWithAttrsVO.setIcon(attrGroupEntity.getIcon());
                    attrGroupWithAttrsVO.setCatelogId(attrGroupEntity.getCatelogId());

                    List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrsVO.getAttrGroupId()).stream().filter(
                            attrEntity -> attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()
                    ).collect(Collectors.toList());
                    attrGroupWithAttrsVO.setAttrs(relationAttr);
                    return attrGroupWithAttrsVO;
                }).collect(Collectors.toList());
        return collect;
    }
}